package ali;

/*
 * @Author: qianmingtian
 * @Date: 2020-05-29 12:02:38
 * @LastEditors: qianmingtian
 * @LastEditTime: 2020-07-08 09:31:42
 * @FilePath: \qianmingtian.github.io\code\ali\MinimumCost.java
 */ 
import java.util.Scanner;

public class MinimumCost {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int T = in.nextInt();

        for(int k=0; k<T; k++) {

            long n = in.nextInt();
            long m = in.nextInt();
            long w2 = in.nextInt();
            long w3 = in.nextInt();

            long startTime = System.currentTimeMillis();
            System.out.println(startTime);

            int upper = (int) Math.ceil(Math.log(m/n)/Math.log(2));
            int lower = (int) Math.ceil(Math.log(m/n)/Math.log(3));
            System.out.println(upper+", "+lower);

            if (n >= m) {
                System.out.println(0);
            } else {

                double result = 0;
                double sumCost = Double.POSITIVE_INFINITY;

                for (int x=lower; x<=upper; x++) {
                    for (int i=0; i<=x; i++) {
                        result = n*Math.pow(2,i)*Math.pow(3,x-i);
                        if(result>= m) {
                            double cost = w2*i + w3*(x-i);
                            if(cost < sumCost) {
                                sumCost = cost;
                            }
                        }
                    }
                }
                System.out.println("minimum cost: " + (int)sumCost);
                long endTime = System.currentTimeMillis();
                System.out.println(endTime);
                System.out.println(endTime-startTime);
            }
        }
        in.close();
    }
}